IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Member]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[Member](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			Code VARCHAR(255) NOT NULL,
			Name VARCHAR(50) NOT NULL,
			FirstName VARCHAR(50) NOT NULL,
			Birthday DATE NOT NULL,
			Address VARCHAR(120) NOT NULL,
			City VARCHAR(80) NOT NULL,
			Cp VARCHAR(5) NOT NULL,
			Email VARCHAR(80) NOT NULL,
			Pwd VARCHAR(MAX) NOT NULL,
			Img VARCHAR(100) NULL,
			Sexe CHAR(1) NOT NULL,
			DriveLicenceDate DATE NULL,
			Activated BIT NOT NULL DEFAULT(0),
			DateCreation DATETIME NOT NULL DEFAULT GETDATE(),
			DateUpdate DATETIME NOT NULL DEFAULT GETDATE()
		);
		
		PRINT N' Table Member was created.'
	END
ELSE
	BEGIN
		PRINT N' Table Member exists yet.'
	END

	
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Event]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[Event](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			Title VARCHAR(50) NOT NULL,
			NamePlace VARCHAR(80) NOT NULL,
			Description VARCHAR(MAX) NULL,
			Img VARCHAR(100) NULL,
			Price INT NULL,
			Address VARCHAR(120) NOT NULL,
			City VARCHAR(80) NOT NULL,
			Cp VARCHAR(5) NOT NULL,
			DateBegin DATETIME NOT NULL,
			DateEnd DATETIME NOT NULL,
			SiteLink VARCHAR(100) NULL,
			Activated BIT NOT NULL DEFAULT(0),
			DateCreation DATETIME NOT NULL DEFAULT GETDATE(),
			DateUpdate DATETIME NOT NULL DEFAULT GETDATE()
		);
		
		PRINT N' Table Event was created.'
	END
ELSE
	BEGIN
		PRINT N' Table Event exists yet.'
	END
	
	
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Way]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[Way](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			CodeWay VARCHAR(40) NOT NULL,
			Contribution INT NOT NULL DEFAULT(0),
			MeetingHour VARCHAR(8) NOT NULL,
			MeetingPlace VARCHAR(250) NOT NULL,
			NumberPlaces INT NOT NULL,
			DriverId INT NOT NULL,
			EventId INT NOT NULL,
			VehiculeId INT NOT NULL,
			DateCreation DATETIME NOT NULL DEFAULT GETDATE(),
			DateUpdate DATETIME NOT NULL DEFAULT GETDATE()
		); 
		PRINT N' Table Way was created.'
	END
ELSE
	BEGIN
		PRINT N' Table Way Exists yet.'
	END

	
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PassagerWay]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[PassagerWay](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			PassagerId INT NOT NULL,
			WayId INT NOT NULL,
			DateCreation DATETIME NOT NULL DEFAULT GETDATE(),
			DateUpdate DATETIME NOT NULL DEFAULT GETDATE()
		);
		PRINT N' Table PassagerWay was created.'
	END
ELSE
	BEGIN
		PRINT N' Table PassagerWay Exists yet.'
	END

	
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Survey]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[Survey](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			Note INT NOT NULL,
			Summery VARCHAR(MAX) NULL,
			PassagerId INT NOT NULL,
			WayId INT NOT NULL,
			DateCreation DATETIME NOT NULL DEFAULT GETDATE(),
			DateUpdate DATETIME NOT NULL DEFAULT GETDATE()
		);

		PRINT N' Table Survey was created.'
	END
ELSE
	BEGIN
		PRINT N' Table Survey Exists yet.'
	END

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[VehiculeBrand]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[VehiculeBrand](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			Title VARCHAR(80) NOT NULL,
			DateCreation DATETIME NOT NULL DEFAULT GETDATE(),
			DateUpdate DATETIME NOT NULL DEFAULT GETDATE()
		);

		PRINT N' Table VehiculeBrand was created.'
	END
ELSE
	BEGIN
		PRINT N' Table VehiculeBrand Exists yet.'
	END

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[VehiculeModel]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[VehiculeModel](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			Title VARCHAR(80) NOT NULL,
			VehiculeBrandId INT NOT NULL,
			DateCreation DATETIME NOT NULL DEFAULT GETDATE(),
			DateUpdate DATETIME NOT NULL DEFAULT GETDATE()
		);

		PRINT N' Table VehiculeModel was created.'
	END
ELSE
	BEGIN
		PRINT N' Table VehiculeModel Exists yet.'
	END

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Vehicule]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[Vehicule](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			Matricule VARCHAR(20) NOT NULL,
			NumberPlaces INT NOT NULL,
			MemberId INT NOT NULL,
			VehiculeModelId INT NOT NULL,
			Deleted BIT NOT NULL DEFAULT(0),
			DateCreation DATETIME NOT NULL DEFAULT GETDATE(),
			DateUpdate DATETIME NOT NULL DEFAULT GETDATE()
		);

		PRINT N' Table Vehicule was created.'
	END
ELSE
	BEGIN
		PRINT N' Table Vehicule Exists yet.'
	END

	
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Preference]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[Preference](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			Age INT NULL,
			Smoker BIT NOT NULL DEFAULT(0),
			Music BIT NOT NULL DEFAULT(0),
			MemberId INT NOT NULL,
			DriverStyleId INT NOT NULL,
			DateCreation DATETIME NOT NULL DEFAULT GETDATE(),
			DateUpdate DATETIME NOT NULL DEFAULT GETDATE()
		);

		PRINT N' Table Preference was created.'
	END
ELSE
	BEGIN
		PRINT N' Table Preference Exists yet.'
	END
	
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DriverStyle]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[DriverStyle](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			Title VARCHAR(50) NOT NULL
		);

		PRINT N' Table DriverStyle was created.'
	END
ELSE
	BEGIN
		PRINT N' Table DriverStyle Exists yet.'
	END
/*FOREIGNKEY*/

IF (OBJECT_ID('FK_Way_Member', 'F') IS NULL)
	BEGIN	
		ALTER TABLE [RideEventDB].[dbo].[Way]
		ADD CONSTRAINT FK_Way_Member FOREIGN KEY (DriverId)
		REFERENCES [RideEventDB].[dbo].[Member] (Id);
		
		PRINT N' Foreign key FK_Way_Member was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_Way_Member exists yet.'
	END
	
IF (OBJECT_ID('FK_Way_Event', 'F') IS NULL)
	BEGIN		
		ALTER TABLE [RideEventDB].[dbo].[Way]
		ADD CONSTRAINT FK_Way_Event FOREIGN KEY (EventId)
		REFERENCES [RideEventDB].[dbo].[Event] (Id);
		
		PRINT N' Foreign key FK_Way_Event was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_Way_Event exists yet.'
	END
	
IF (OBJECT_ID('FK_Way_Vehicule', 'F') IS NULL)
	BEGIN		
		ALTER TABLE [RideEventDB].[dbo].[Way]
		ADD CONSTRAINT FK_Way_Vehicule FOREIGN KEY (VehiculeId)
		REFERENCES [RideEventDB].[dbo].[Vehicule] (Id);
		
		PRINT N' Foreign key FK_Way_Vehicule was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_Way_Vehicule exists yet.'
	END

IF (OBJECT_ID('FK_PassagerWay_Member', 'F') IS NULL)
	BEGIN
		ALTER TABLE [RideEventDB].[dbo].[PassagerWay]
		ADD CONSTRAINT FK_PassagerWay_Member FOREIGN KEY (PassagerId)
		REFERENCES [RideEventDB].[dbo].[Member] (Id);
		
		PRINT N' Foreign key FK_PassagerWay_Member was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_PassagerWay_Member exists yet.'
	END
	
IF (OBJECT_ID('FK_PassagerWay_Way', 'F') IS NULL)
	BEGIN
		ALTER TABLE [RideEventDB].[dbo].[PassagerWay]
		ADD CONSTRAINT FK_PassagerWay_Way FOREIGN KEY (WayId)
		REFERENCES [RideEventDB].[dbo].[Way] (Id);
		
		PRINT N' Foreign key FK_PassagerWay_Way was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_PassagerWay_Way exists yet.'
	END
	
IF (OBJECT_ID('FK_Survey_Member', 'F') IS NULL)
	BEGIN
		ALTER TABLE [RideEventDB].[dbo].[Survey]
		ADD CONSTRAINT FK_Survey_Member FOREIGN KEY (PassagerId)
		REFERENCES [RideEventDB].[dbo].[Member] (Id);
		
		PRINT N' Foreign key FK_Survey_Member was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_Survey_Member exists yet.'
	END
	
IF (OBJECT_ID('FK_Survey_Way', 'F') IS NULL)
	BEGIN
		ALTER TABLE [RideEventDB].[dbo].[Survey]
		ADD CONSTRAINT FK_Survey_Way FOREIGN KEY (WayId)
		REFERENCES [RideEventDB].[dbo].[Way] (Id);
		
		PRINT N' Foreign key FK_Survey_Way was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_Survey_Way exists yet.'
	END

IF (OBJECT_ID('FK_Vehicule_VehiculeModel', 'F') IS NULL)
	BEGIN
		ALTER TABLE [RideEventDB].[dbo].[Vehicule]
		ADD CONSTRAINT FK_Vehicule_VehiculeModel FOREIGN KEY (VehiculeModelId)
		REFERENCES [RideEventDB].[dbo].[VehiculeModel] (Id);
		
		PRINT N' Foreign key FK_Vehicule_VehiculeModel was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_Vehicule_VehiculeModel exists yet.'
	END

IF (OBJECT_ID('FK_VehiculeModel_VehiculeBrand', 'F') IS NULL)
	BEGIN
		ALTER TABLE [RideEventDB].[dbo].[VehiculeModel]
		ADD CONSTRAINT FK_VehiculeModel_VehiculeBrand FOREIGN KEY (VehiculeBrandId)
		REFERENCES [RideEventDB].[dbo].[VehiculeBrand] (Id);
		
		PRINT N' Foreign key FK_VehiculeModel_VehiculeBrand was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_VehiculeModel_VehiculeBrand exists yet.'
	END

IF (OBJECT_ID('FK_Vehicule_Member', 'F') IS NULL)
	BEGIN
		ALTER TABLE [RideEventDB].[dbo].[Vehicule]
		ADD CONSTRAINT FK_Vehicule_Member FOREIGN KEY (MemberId)
		REFERENCES [RideEventDB].[dbo].[Member] (Id);
		
		PRINT N' Foreign key FK_Vehicule_Member was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_Vehicule_Member exists yet.'
	END
	
IF (OBJECT_ID('FK_Preference_DriverStyle', 'F') IS NULL)
	BEGIN
		ALTER TABLE [RideEventDB].[dbo].[Preference]
		ADD CONSTRAINT FK_Preference_DriverStyle FOREIGN KEY (DriverStyleId)
		REFERENCES [RideEventDB].[dbo].[DriverStyle] (Id);
		
		PRINT N' Foreign key FK_Preference_DriverStyle was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_Preference_DriverStyle exists yet.'
	END

IF (OBJECT_ID('FK_Preference_Member', 'F') IS NULL)
	BEGIN
		ALTER TABLE [RideEventDB].[dbo].[Preference]
		ADD CONSTRAINT FK_Preference_Member FOREIGN KEY (MemberId)
		REFERENCES [RideEventDB].[dbo].[Member] (Id);
		
		PRINT N' Foreign key FK_Preference_Member was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_Preference_Member exists yet.'
	END
